﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Galoap
{
    abstract partial class Document
    {
        public static List<T> GetDocuments<T>() where T : Document
        {
            /* List<PhraseDocument> l = GaloapDatabase.Database.Document.OfType<PhraseDocument>().ToList();
            foreach (PhraseDocument t in l)
                Console.WriteLine(((PhraseDocument)t).Phrase);*/
            return GaloapDatabase.Database.Document.OfType<T>().ToList();
        }

        public static IEnumerable<T> GetEnumerator<T>() where T : Document
        {
            foreach (T document in GetDocuments<T>()) yield return document;
        }

        public static IEnumerable<T> GetRandomEnumerator<T>() where T : Document
        {
            foreach (var document in GetDocuments<T>().OrderBy(d => Guid.NewGuid())) yield return document;
        }

        /// <summary>
        /// Adds this to the database
        /// </summary>
        public void AddDocumentToDB()
        {
            GaloapDatabase.Database.AddToDocument(this);
            GaloapDatabase.Database.SaveChanges();
        }

        /// <summary>
        /// Saves the current state of this in the database
        /// </summary>
        public void Save()
        {
            GaloapDatabase.Database.SaveChanges();
        }

        abstract override public String ToString();
    }
}
